<!-- ERP 产品库存列表 -->
<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="产品名称" prop="productId">
        <el-select
          v-model="queryParams.productId"
          clearable
          filterable
          placeholder="请选择产品"
          class="!w-240px"
        >
          <el-option
            v-for="item in productList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="产品条码" prop="barCode">
        <el-input
          v-model="queryParams.barCode"
          placeholder="请输入名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="产品分类" prop="categoryId">
        <el-tree-select
          v-model="queryParams.categoryId"
          :data="categoryList"
          :props="defaultProps"
          check-strictly
          default-expand-all
          placeholder="请选择产品分类"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="产品单位" prop="productUnitId">
        <el-select
          v-model="queryParams.productUnitId"
          clearable
          filterable
          placeholder="请选择产品单位"
          class="!w-240px"
        >
          <el-option
            v-for="item in productUnitList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="仓库" prop="warehouseId">
        <el-select
          v-model="queryParams.warehouseId"
          clearable
          filterable
          placeholder="请选择仓库"
          class="!w-240px"
        >
          <el-option
            v-for="item in warehouseList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="产品条码" align="center" prop="barCode" />
      <el-table-column label="产品分类" align="center" prop="categoryName" />
      <el-table-column label="产品单位" align="center" prop="productUnitName" />
      <el-table-column label="可用库存" align="center" prop="count" />
      <el-table-column label="锁定库存" align="center" prop="lockCount" />
      <!-- <template #default="scope">
          <span
            v-if="scope.row.lockCount > 0"
            class="text-primary cursor-pointer"
            @click="openLockForm(scope.row.productId, scope.row.warehouseId)"
            >{{ scope.row.lockCount }}</span
          >
          <span v-else>{{ scope.row.lockCount }}</span>
        </template> 
      </el-table-column>
      -->
      <el-table-column label="生产中库存" align="center" prop="pendingStorageCount">
        <template #default="scope">
          <span
            v-if="scope.row.pendingStorageCount > 0"
            class="text-primary cursor-pointer"
            @click="openPendingForm(scope.row.productId, scope.row.warehouseId)"
            >{{ scope.row.pendingStorageCount }}</span
          >
          <span v-else>{{ scope.row.pendingStorageCount }}</span>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
  <!-- 锁定库存 -->
  <LockStockDetail ref="lockRef" @success="getList" />
  <!-- 生产中库存 -->
  <PendingStockDetail ref="pendingRef" @success="getList" />
</template>

<script setup lang="ts">
import { StockApi, StockVO } from '@/api/erp/stock/stock'
import { ProductApi, ProductVO } from '@/api/erp/product/product'
import { WarehouseApi, WarehouseVO } from '@/api/erp/stock/warehouse'
import { ProductCategoryApi, ProductCategoryVO } from '@/api/erp/product/category'
import { ProductUnitApi, ProductUnitVO } from '@/api/erp/product/unit'
import LockStockDetail from './components/LockStockDetail.vue'
import PendingStockDetail from './components/PendingStockDetail.vue'

/** ERP 产品库存列表 */
defineOptions({ name: 'ErpStock' })

const loading = ref(true) // 列表的加载中
const list = ref<StockVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  productId: undefined,
  barCode: undefined,
  categoryId: undefined,
  productUnitId: undefined,
  warehouseId: undefined,
  type: 'finished'
})
const queryFormRef = ref() // 搜索的表单
const productList = ref<ProductVO[]>([]) // 产品列表
const warehouseList = ref<WarehouseVO[]>([]) // 仓库列表
const categoryList = ref<ProductCategoryVO[]>([]) // 分类列表
const productUnitList = ref<ProductUnitVO[]>([]) // 产品单位列表
const defaultProps = {
  label: 'name',
  children: 'children',
  value: 'id'
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await StockApi.getStockPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}
/** 锁定库存详情页 */
const lockRef = ref()
// const openLockForm = (productId: number, warehouseId: number) => {
//   lockRef.value.open(productId, warehouseId)
// }
/** 生产中库存详情页 */
const pendingRef = ref()
const openPendingForm = (productId: number, warehouseId: number) => {
  pendingRef.value.open(productId, warehouseId)
}
/** 初始化 **/
onMounted(async () => {
  await getList()
  // 加载产品、仓库列表
  productList.value = await ProductApi.getProductSimpleList()
  warehouseList.value = await WarehouseApi.getWarehouseSimpleList()
  categoryList.value = await ProductCategoryApi.getProductCategorySimpleList({ type: 'finished' })
  productUnitList.value = await ProductUnitApi.getProductUnitSimpleList({ type: 'finished' })
})
</script>
